<template>
  <!-- 问题分类弹窗 -->
  <a-modal :title="headerTitle" :visible.sync="dialogVisible" @cancel="handleCancel" width="900px">
    <a-spin :spinning="spinning">
      <!-- 表单 -->
      <a-form-model ref="ruleForm" :model="forms" :rules="rules" :labelCol="{ span: 8 }" :wrapperCol="{ span: 14 }">
        <div id="dialogDistrictBox">
          <a-row>
            <a-col :span="12">
              <a-form-model-item prop="subItemName" label="所属分项工程">
                <BaseInput disabled v-model="forms.subItemName"></BaseInput>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item prop="periods" label="整改期限（天）">
                <BaseInputNumber
                  :precision="0"
                  :min="0"
                  :max="100000"
                  :disabled="editModel == '2'"
                  v-model="forms.periods"
                ></BaseInputNumber>
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item prop="securityRisksName" label="安全隐患描述">
                <BaseInput
                  :maxLength="300"
                  type="textarea"
                  :rows="6"
                  :disabled="editModel == '2'"
                  v-model="forms.securityRisksName"
                ></BaseInput>
              </a-form-model-item>
            </a-col>
            <!-- <a-col :span="24">
              <a-form-model-item
                prop="securityRisksDescription"
                label="安全隐患描述"
                :labelCol="{ span: 4 }"
                :wrapperCol="{ span: 19 }"
              >
                <BaseInput
                  :disabled="editModel == '2'"
                  v-model="forms.securityRisksDescription"
                  type="textarea"
                  :rows="6"
                />
              </a-form-model-item>
            </a-col> -->
          </a-row>
        </div>
      </a-form-model>
    </a-spin>
    <!-- 页脚操作栏 -->
    <div slot="footer" class="btn_box">
      <a-button :loading="loading" v-if="editModel != 2" type="primary" class="button_right" @click="handleOk"
        >提交</a-button
      >
      <a-button type="primary" @click="handleCancel">关闭</a-button>
    </div>
  </a-modal>
</template>

<script>
import { risksList } from '@/api/quality/safe'
import cloneDeep from 'lodash.clonedeep' // 深拷贝
export default {
  name:'a'+ Date.now(),
  props: {
    // 显隐控件
    visible: {
      type: Boolean,
      default() {
        return false
      },
    },
    // 操作类型
    editModel: {
      type: [String, Number],
      default() {
        return 0
      },
    },
    // 表单
    form: {
      type: Object,
      default() {
        return {}
      },
    },
    // 标题
    headerTitle: {
      type: String,
      default() {
        return ''
      },
    },
  },
  computed: {
    // 与父组件绑定visible
    dialogVisible: {
      get: function () {
        return this.visible
      },
      set: function (e) {
        this.$emit('update:visible', e)
      },
    },
  },
  data() {
    return {
      loading: false,
      spinning: false,
      forms: {},
      rules: {
        subItemName: [{ required: true, message: '请选择所属分项工程', trigger: 'change' }],
        // securityRisksName: [{ required: true, whitespace:true,message: '请输入安全隐患名称', trigger: 'change' }],
        periods: [{ required: true, message: '请输入整改期限', trigger: 'change' }],
        securityRisksName: [{ required: true, whitespace:true,message: '请输入安全隐患描述', trigger: 'change' }],
      },
    }
  },
  methods: {
    handleOk() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.loading = true
          this.$emit('confirm', this.forms)
        } else {
            this.$message.warning("请填写完所有必填项信息")
          return false
        }
      })
    },
    handleCancel() {
      this.dialogVisible = false
    },
    handleChange() {},
  },
  watch: {
    visible(e) {
      if (e) {
        this.$nextTick(() => {
          // 若操作为查看和编辑
          if (this.editModel != 0) {
            this.spinning = true
            risksList
              .getDetail({
                id: this.form.id,
              })
              .then((res) => {
                this.spinning = false
                if (res.code == 200) {
                  this.forms = res.data
                }
              })
              .catch((res) => {
                this.spinning = false
              })
          } else {
            this.forms = cloneDeep(this.form)
          }
        })
      } else {
        this.forms = {}
        // 初始化表单
        this.$refs.ruleForm.clearValidate()
      }
    },
  },
}
</script>

<style>
#dialogDistrictBox .ant-checkbox-group .ant-form-item label {
  position: relative !important;
}
</style>
